<template>
  <div class="login">
    <div class="login-box">
        <div class="avatar" id="identifier">
            <img src="../assets/logo.png" alt="">
        </div>
        <!-- 表单区域 -->
        <el-form ref="loginFormRef" class="login_form" :model="loginForm" :rules="loginrules">
            <el-form-item prop="username">
                <el-input  prefix-icon="iconfont icon-user" v-model='loginForm.username' ></el-input>
            </el-form-item>
               <el-form-item  prop="password" >
                <el-input prefix-icon="iconfont icon-3702mima" v-model="loginForm.password" type="password" ></el-input>
            </el-form-item>
              <el-form-item style="float:right">
               <el-button type="primary" @click="login">登录</el-button>
                <el-button type="info" @click="resetForm">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
  </div>
</template>

<script>
export default {

  data () {
    return {
        // 实现表单数据双向绑定
        loginForm:{
            username:'admin',
            password:'123456'
        },
        // 验证表单 给表单绑定数据 通过rules
        loginrules:{
            username:[
                 { required: true, message: '请输入活动名称', trigger: 'blur' },
            { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
            ],
            password:[  { required: true, message: '请输入活动名称', trigger: 'blur' },
            { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }]
        }
    }
  },

  methods: {
    //  将表单重置  需要在表单上使用ref 创建一个实例 使用this.$refs调用
      resetForm(){
          this.$refs.loginFormRef.resetFields()
      },
    //   点击登录 调用login函数 对表单进行预效验 
      login(){
          this.$refs.loginFormRef.validate( async falg=>{
              if(!falg) return;
            //   发送请求 获取登录状态
              const res= await this.axios.post("login",this.loginForm)
            //   调用挂载在vue原型上的放法 弹框
                if(res.meta.status != 200) return this.$mg({type:'error',
                message:res.meta.msg,
                offset:100
                })
                 this.$mg.success('登录成功');
                 sessionStorage.setItem('token',res.data.token);
                 this.$router.push('/home')
          })
      }
  },

  created () {
  }
}
</script>

<style lang='less' scoped>

.login{
    width: 100%;
    height: 100%;
     background: #2b4b6b;
}
.login-box{
    width: 450px;
    height: 300px;
    background: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform:translate(-50%,-50%)
}
.avatar{
    width: 130px;
    height: 130px;
    border: 2px solid #ff2123;
    border-radius: 50%;
    padding: 10px;
    position: absolute;
    left: 50%;
     transform:translate(-50%,-50%);
     background-color: pink;
     box-shadow: 0 0 50px yellow;
   
    img{
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: lightsalmon;
    }
}
.login_form{
    position: absolute;
    bottom: 0;
    width: 100%;
    box-sizing: border-box;
    padding:0 20px;
}
</style>
